#====导入模块====#
import numpy as np # 导入numpy包 
from sklearn.linear_model import LinearRegression        # 导入线性回归模型模块
from sklearn.metrics import mean_squared_error, r2_score # 导入度量标准模块

#====数据准备====#
a = np.array([1, 6]) # (2,) # 自定义真实模型参数
b = 5
X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]]) # 定义输入变量X
# y = a_0 * x_0 + a_1 * x_1 + b
y = np.dot(X, a) + b        # 根据模型输出变量y

#====配置模型====#
lr = LinearRegression() # 创建线性回归模型
lr.fit(X, y)            # 模型训练
[a_hat, b_hat]= [lr.coef_, lr.intercept_] # lr模型参数
print('lr斜率: a = ', a_hat)
print('lr截距：b = %.2f' %b_hat)
y_pred = lr.predict(X)  # 模型预测
print('预测结果：y_pred = ', y_pred)

#====模型评估====#
print('均方根误差: mse = %.2f' % mean_squared_error(y, y_pred)) # The mean squared error
print('决定系数: r2 = %.2f' % r2_score(y, y_pred))              # 等价于 r2 = lr.score(X, y)